<template>
	<div>
		<el-card>
			<div slot="header" class="clearfix">
				<span>修改密码</span>
			</div>
			<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
				<el-form-item label="原密码" prop="beforepass">
					<el-input type="password" v-model="ruleForm.beforepass" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="新密码" prop="pass">
					<el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="确认密码" prop="checkPass">
					<el-input type="password" v-model="ruleForm.checkPass"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="submitForm">提交</el-button>
					<el-button @click="resetForm('ruleForm')">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
	</div>
</template>

<script>
	import api from '@/api'
	export default {
		data() {
			var validatePass3 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入原密码'));
				}
			};
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请输入密码'));
				} else {
					if (this.ruleForm.checkPass !== '') {
						this.$refs.ruleForm.validateField('checkPass');
					}
					callback();
				}
			};
			var validatePass2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else if (value !== this.ruleForm.pass) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			return {
				ruleForm: {
					pass: '',
					checkPass: '',
					beforepass: ''
				},
				beforepassword: '',
				id: '',
				rules: {
					pass: [{
						validator: validatePass,
						trigger: 'blur'
					}],
					checkPass: [{
						validator: validatePass2,
						trigger: 'blur'
					}],
					beforepass: [{
						validator: validatePass3,
						trigger: 'blur'
					}]
				}
			};
		},
		methods: {
			async submitForm() {
				if (this.beforepassword !== this.ruleForm.beforepass) {
					this.$message('请输入原密码');
				} else if (this.ruleForm.pass == this.ruleForm.checkPass) {
					let name = localStorage.getItem('username')
					let userdata = {
						data: {
							id: this.id,
							username: name,
							password: this.ruleForm.checkPass
						}
					}
					let res = await api.userinfo.exituser(userdata)
					if (res.data.msg === 'success') {
						this.$message({
							message: '修改成功',
							type: 'success'
						});
					}
				} else {
					return
				}
			},
			async getUserinfo() {
				this.id = localStorage.getItem('userID')
				let data = {
					id: this.id,
					page: 1,
					size: 1,
				}
				let res = await api.userinfo.getuserinfo(data)
				if (res.data.data.length !== 0) {
					this.beforepassword = res.data.data[0].password
				} else {
					this.$message({
						type: 'warning',
						message: '用户信息有误'
					})
				}
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		},
		created() {
			this.getUserinfo()
		}
	}
</script>


<style>

</style>
